{% extends 'base.html' %}


{% block room %}
    <div class="col-xs-2 col-sm-2 col-md-2">
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <div class="page-header">
                <h3 class="text-center">
                    <img src="{{ data.portrait }}" class="img-circle" style="width: 40px; height: 40px;">
                    <a href="{% url 'userprofile' username %}" style="color: #0f0f0f">{{ username }}</a>
                </h3>
            </div>
            <div style="background-color: #bfbcbc;" class="text-center">
                <ul class="nav nav-pills nav-stacked">
                    <li class="active"><a href="#">聊天系统<span class="sr-only">(current)</span></a></li>
                    <li><a href="{% url 'back2chat' username %}" style="color: #333">聊天室</a></li>
                    <li>
                        <form method="POST" action="/logout/">
                            <input type="submit" class="btn navbar-btn" style="background-color: #bfbcbc;" value="退出登录"/>
                            <input type="hidden" name="username" value={{ username }}>
                            {% csrf_token %}
                        </form>
                    </li>
                </ul>
            </div>
        </div>
    </div>

    <div class="col-xs-7 col-md-7 col-sm-7" style="margin-top: 15px; padding-right: 0px">
        <div id="room" style="height: 450px">
            <div class="panel panel-default">
                <div class="panel-heading">
                    聊天室：{{ roomname }}
                </div>
                <div class="panel-body" style="height: 350px; padding: 0px; ">
                        <textarea id="chat-log" style="height: 100%; width: 100%; border-color: #f5f5f5;">

                        </textarea>

                </div>
                <div class="panel-footer" style="padding: 0px">
                    <input id="chat-message-input" type="text"
                           style="width: 100%; height: 120px; border: #f5f5f5; border-top-color: #dff0d8; "/>
                    <div class="col-md-offset-11">
                        <input id="chat-message-submit" class="btn btn-primary" type="button" value="发送"
                               style="margin-top: 10px;margin-bottom: 10px; "/>
                    </div>

                </div>
            </div>
        </div>
    </div>
    <div class="col-xs-3">
        <div class="panel panel-default text-center" style="margin-top: 15px">
           <div class="panel-heading" role="alert" >在线</div>
           <ul class="list-group">
              {% for user in online %}
                    {% if user.username == username %}
                    {% else %}
                        <li class="list-group-item">
                            <div>
                                <a href="{% url 'friendprofile' username user.username %}" class="list-group-item">
                                    <img src="{{ user.portrait }}" class="img-circle img-thumbnail" style="margin: 2px;width: 30px;height: 30px">
                                    {{ user.username }}
                                </a>
                            </div>
                        </li>
                    {% endif %}
                {% endfor %}
           </ul>
        </div>
        <div class="panel panel-default text-center">
           <div class="panel-heading" role="alert">离线</div>
           <ul class="list-group" style="margin-top: 15px">
                {% for user in offline %}
                    {% if user.username == username %}
                    {% else %}
                        <li class="list-group-item">
                            <a href="{% url 'friendprofile' username user.username %}" class="list-group-item">
                                <img src="{{ user.portrait }}" class="img-circle img-thumbnail" style="margin: 5px;width: 40px;height: 40px">
                                {{ user.username }}</a>
                        </li>
                    {% endif %}
                {% endfor %}
            </ul>
        </div>
    </div>
{% endblock room %}


{% block script %}
    <script>
        var roomName = {{ room_json }};
        var userName = {{ user_json }};

        var chatSocket = new WebSocket(
            'ws://' + window.location.host +
            '/ws/chat/' + roomName + '/' + userName + '/');
        console.log(chatSocket)
        chatSocket.onmessage = function (e) {
            var data = JSON.parse(e.data);
            var message = data['message'];
            document.querySelector('#chat-log').value += (message + '\n');
        };

        chatSocket.onclose = function (e) {
            console.error('Chat socket closed unexpectedly');
        };

        document.querySelector('#chat-message-input').focus();
        document.querySelector('#chat-message-input').onkeyup = function (e) {
            if (e.keyCode === 13) {  // enter, return
                document.querySelector('#chat-message-submit').click();
            }
        };

        document.querySelector('#chat-message-submit').onclick = function (e) {
            var messageInputDom = document.querySelector('#chat-message-input');
            var message = userName + ':\t' + messageInputDom.value;
            chatSocket.send(JSON.stringify({
                'message': message
            }));

            messageInputDom.value = '';
        };
    </script>
{% endblock script %}
